<template>
  <div>
    <!-- 一条文章单元格 -->
    <van-cell>
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{ obj.title }}</span>
          <!-- 单图 -->
          <img class="thumb" v-lazy="obj.cover.images[0]" v-if="obj.cover.type === 1" />
        </div>
        <!-- 三张图片 -->
        <div class="thumb-box" v-if="obj.cover.type > 1">
          <img class="thumb" v-for="(imgUrl, index) in obj.cover.images" :key="index" v-lazy="imgUrl" />
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <div>
            <span>作者：{{ obj.aut_name }}</span>
            <span>{{ obj.comm_count }} 评论</span>
            <span>时间日期：{{ obj.pubdate }}</span>
          </div>
          <!-- 反馈按钮 -->
          <van-icon name="cross" @click.stop="onCloseClick" v-if="isShowX" />
        </div>
      </template>
    </van-cell>
    <!-- 反馈面板 -->
    <van-action-sheet v-model="show" :actions="actions" @select="onSelect" get-container="body" cancel-text="取消" />
    <!-- 二反馈面板 -->
    <van-action-sheet v-model="show2" :actions="actions2" @select="onSelect" get-container="body" cancel-text="返回" />
  </div>
</template>

<script>
import { Notify } from 'vant'
import { articleDislikeAPI, articleReportsAPI } from '@/api/home.js'
import moment from 'moment'
import { firstActions, secondActions } from '@/constant/'
export default {
  data() {
    return {
      show: false,
      show2: false,
      // actions: [{ name: '不感兴趣' }, { name: '反馈垃圾内容' }, { name: '拉黑作者' }]
      actions: firstActions,
      actions2: secondActions
    }
  },
  name: 'articleitem',
  props: {
    obj: {
      type: Object,
      // 设置default为对象格式时，要用函数表示
      default() {
        return {}
      }
    },
    isShowX: {
      type: Boolean,
      default: true // 默认显示
    }
  },
  created() {
    this.obj.pubdate = this.timeAgo(this.obj.pubdate)
  },
  methods: {
    timeAgo(dat) {
      moment.locale('zh-cn')
      const a = moment([dat])
      return a.from(Date.now()) // "1 天前"
    },
    // 点击了叉号按钮
    onCloseClick() {
      // 展示动作面板
      this.show = true
    },
    async onSelect(actions, actions2) {
      // action绑定的数据对象, index是索引
      if (actions.name === '反馈垃圾内容') {
        // 证明点击的是反馈垃圾内容
        this.show2 = true
      } else if (actions.name === '不感兴趣') {
        try {
          // 1发请求通知后台
          await articleDislikeAPI({
            artid: this.obj.art_id
          })
          Notify({ type: 'success', message: '反馈成功' })
        } catch (err) {
          Notify({ type: 'warning', message: '反馈失败-联系程序员' })
        }
        // 2通知父组件，把当前的文章对象从数组中删掉
        this.$emit('dislike', this.obj)
        this.show = false // 关闭弹窗
      } else {
        try {
          await articleReportsAPI({
            artid: this.obj.art_id,
            type: String(actions2.value),
            // axios内部如果看见了null，那么这个字段就不会携带给后台
            remark: actions2.value === 0 ? actions2.name : null
          })
          Notify({ type: 'success', message: '举报成功' })
        } catch (err) {
          Notify({ type: 'warning', message: 'err' || '举报失败' })
        }
        this.show = this.show2 = false
      }
      console.log(actions, actions2)
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
